#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Description: 注解
from utils.logger import Logger

log = Logger()


def replicas_single(order: int, description: str, scene: str):
    """标记测试方法在副本数为1时执行"""
    def decorator(func):
        # 使用 _replica_tests 列表存储多个测试配置
        if not hasattr(func, '_replica_tests'):
            func._replica_tests = []
        func._replica_tests.append({
            'replicas': 1,
            'order': order,
            'description': description,
            'scene': scene
        })
        return func
    return decorator


def replicas_double(order: int, description: str, scene: str):
    """标记测试方法在副本数为2时执行"""
    def decorator(func):
        # 同样使用 _replica_tests 存储配置
        if not hasattr(func, '_replica_tests'):
            func._replica_tests = []
        func._replica_tests.append({
            'replicas': 2,
            'order': order,
            'description': description,
            'scene': scene
        })
        return func
    return decorator


def run_tests(test_class, args):
    # 创建测试类的实例，并传入 args
    test_instance = test_class(parse_args=args)
    methods = []

    # 收集所有测试方法及其配置
    for method_name in dir(test_instance):
        if not method_name.startswith('test_'):
            continue
        method = getattr(test_instance, method_name)
        if callable(method):
            # 获取该方法上的所有副本配置
            configs = getattr(method, '_replica_tests', [])
            for config in configs:
                replicas = config.get('replicas')
                order = config.get('order', 0)
                description = config.get('description', "No description")
                scene = config.get('scene', None)
                methods.append((replicas, order, description, scene, method_name, method))

    # 根据传入的 scene 和 replicas 参数筛选配置
    selected_methods = []
    for req, order, description, scene, name, method in methods:
        # 严格匹配 scene
        if args.scene != scene:
            continue
        # 必须匹配 replicas
        if args.replicas != req:
            continue
        selected_methods.append((req, order, description, scene, name, method))

    # 按顺序排序：先按 order，再按副本数
    selected_methods.sort(key=lambda x: (x[1], x[0]))

    # 执行测试方法
    for req, order, description, scene, name, method in selected_methods:
        log.info(f"===== Running {name} =====")
        log.info(f"Scene: {scene}；Description: {description}")
        log.info("----- Start -----")
        method()
        log.info("----- End -----\n")